<template>
	<div>
	
	</div>

</template>

<script>
	import {
		mapActions
	} from 'vuex'
	import invenorycomment from '../components/InventoryDataList.vue'
	import uploadfile from '../../mes/components/UploadFile.vue'
	import load_project_equi from '../components/ProjectEquiDataList.vue'
	import supplierdatalist from '../components/SupplierDataList.vue'
	import showfiledialog from '../../comments/ShowFilesDialog.vue'
	 
	export default {
		name: 'edit_electrical_info',
		components: {
			uploadfile,
			invenorycomment,
			showfiledialog,
			projectequi: load_project_equi,
			supplierdatalist: supplierdatalist
		},
		data: () => ({
			fileDialogComment: {
				dialog: false,
				files: []
			},
			supplierCommentModel: {
				dialog: false,
				loadSupplierUrl: `PDM/PDMDataElectrical/LoaSupplierManagerdPage`
			},
			NeedQtyRule: [
				v => /^[0-9]*$/.test(v) || '请输入合法数字',
				v => v > 0 || '需求量不能小于0'
			],
			bomQuery: {
				name: '',
				code: '',
				bomtype: 1,
				bomstatus: 1,
			},
			enums: ['BomStatus', 'BomType', 'WorkOrderType'],
			bomStatusItems: [],
			bomTypeItems: [],
			workOrderTypeItems: [],
			modelUseBomTypeItems: [],
			bomHeaderList: [],
			bomDataList: [],
			bomTotalCount: 0,
			bomLoading: true,
			bomOptions: { //页数
				page: 1,
				//每页行数
				itemsPerPage: 10
			},
			vuetifyDefaultPage: {
				'items-per-page-text': '每页个数',
				'items-per-page-options': [10, 20, 50, 100]
				// 'items-per-page-all-text': '100'
			},
			bomTreeSearch: '',
			selectionBomTrees: [],
			bomTrees: [],
			assemblydateMenu: false,
			buymaterialdateMenu: false,
			//主BOM
			editFatherDialog: false,
			bomFatherModel: {},
			UpLoadFatherBomPic: {
				isUploadSingleImage: false,
				isUploadMultipleFiles: true,
				type: 30
			},
			UpLoadChildrenBomPic: {
				isUploadSingleImage: false,
				isUploadMultipleFiles: true,
				type: 40
			},
			//子BOM
			editBomChildrenDialog: false,
			bomChildrenModel: {},
			//主物料弹出框

			materialDefaultComment: {
				dialog: false,
				bself: true, // 是否自制
				bpurchase: null, // 是否采购
				bproducing: null, // 是否在制
				loadInventoryPageUrl: `PDM/PDMDataElectrical/LoadInventoryPage`,
				loadInventoryClassUrl: `PDM/PDMDataElectrical/loadInventoryClass`,
				loadUnitsUrl: `PDM/PDMDataElectrical/loadUnits`,
				queryInvClassTreeUrl: `PDM/PDMDataElectrical/GetInvclassTree?iinvcgrade=`
			},
			//替代料弹出框
			materialReplaceComment: {
				dialog: false,
				bself: true, // 是否自制
				bpurchase: null, // 是否采购
				bproducing: null, // 是否在制
				loadInventoryPageUrl: `PDM/PDMDataElectrical/LoadInventoryPage`,
				loadInventoryClassUrl: `PDM/PDMDataElectrical/loadInventoryClass`,
				loadUnitsUrl: `PDM/PDMDataElectrical/loadUnits`,
				queryInvClassTreeUrl: `PDM/PDMDataElectrical/GetInvclassTree?iinvcgrade=`
			},
			//实际用料弹出框
			materialRealComment: {
				dialog: false,
				bself: true, // 是否自制
				bpurchase: null, // 是否采购
				bproducing: null, // 是否在制
				loadInventoryPageUrl: `PDM/PDMDataElectrical/LoadInventoryPage`,
				loadInventoryClassUrl: `PDM/PDMDataElectrical/loadInventoryClass`,
				loadUnitsUrl: `PDM/PDMDataElectrical/loadUnits`,
				queryInvClassTreeUrl: `PDM/PDMDataElectrical/GetInvclassTree?iinvcgrade=`
			},
			inventoryModel: {},
			treePage: {
				totalPages: 0,
				pageNumber: 1,
				pageSize: 10,
				totalRecords: 0
			},
			equiModel: {
				dialog: false,
				url: ``
			},
		}),
		watch: {
			bomOptions: {
				handler() {
					this.loadPages()
				},
				deep: true
			},
		},
		created() {
			this.getHeaderList()
			this.loadEnums()
			this.changeTreePage(1)
		},
		methods: {
			...mapActions('mes/common', [
				//获取物料信息
				'LoadType'
			]),
			//查看图片
			openLookPicsClick(pathstrs) {
				this.fileDialogComment.dialog = true
				this.fileDialogComment.files =JSON.parse(pathstrs)
			},
			async loadEnums() {
				var items = await this.LoadType({
					pramterEnums: this.enums
				})
				this.bomStatusItems = items.filter(r => r.enumName == this.enums[0])
				this.bomTypeItems = items.filter(r => r.enumName == this.enums[1])
				this.modelUseBomTypeItems = items.filter(r => r.enumName == this.enums[1])
				this.workOrderTypeItems = items.filter(r => r.enumName == this.enums[2])
			},
			async changeTreePage(pageNumber) {
				this.treePage.pageNumber = pageNumber
				await this.GetBomTree()
			},
			async GetBomTree() {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataElectrical/GetBomTree?isOpenLock=false&codeOrName=${this.bomTreeSearch}&type=1&pageNumber=${this.treePage.pageNumber}&pageSize=${this.treePage.pageSize}`,
					method: 'post'
				})

				if (response.status == 1) {
					this.bomTrees = response.data.trees
					this.treePage.totalPages = response.data.pagination.totalPages
					this.treePage.totalRecords = response.data.pagination.totalRecords
				} else {
					this.$message.error(response.info)
				}
			},
			async loadPages(keypaths = []) {
				this.bomLoading = true
				var response = await this.$httpRequest({
					url: `PDM/PDMDataElectrical/LoadPage?code=${this.bomQuery.code}&name=${this.bomQuery.name}&bomstatus=${this.bomQuery.bomstatus}&bomtype=${this.bomQuery.bomtype}&type=1&pageNumber=${this.bomOptions.page}&pageSize=${this.bomOptions.itemsPerPage}`,
					method: 'post',
					data: keypaths
				})
				if (response.status == 1) {
					this.bomDataList = response.data.result
					this.bomTotalCount = response.data.pagination.totalRecords
					this.bomLoading = false
				} else {
					this.$message.error(response.info)
				}
			},
			async refreshLoadPages() {
				this.bomQuery.name = ''
				this.bomQuery.code = ''
				this.bomQuery.bomtype = null
				this.bomQuery.bomstatus = null

			},
			selectTreeEvent(tree) {

				if (tree != null && tree.length > 0) {
					var keypaths = tree.map(r => r.keypath)
					this.refreshLoadPages()
					this.loadPages(keypaths)
				}
			},
			//新增下级节点
			addNextNodel(item) {
				this.editBomChildrenDialog = true
				this.bomChildrenModel = {}
				this.bomChildrenModel.parent = item
				//设备
				if (item.bomtype == 1) {
					this.modelUseBomTypeItems = this.modelUseBomTypeItems.filter(r => r.value == 2)
				} else {
					this.modelUseBomTypeItems = this.bomTypeItems
				}
			},
			//编辑 bom设备、部件对话框
			updateBomClick(item) {
				//设备
				if (item.bomtype == 1) {
					this.editFatherDialog = true
					this.bomFatherModel = item
				} else {
					this.bomTypeSelectEvent(this.bomChildrenModel.bomtype)
					this.editBomChildrenDialog = true
					this.bomChildrenModel = item
					this.bomChildrenModel.tempbomtypename = this.bomChildrenModel.bomtypename
					this.bomChildrenModel.tempparentname = this.bomChildrenModel.parentname

				}
			},
			//删除BOM
			async deleteBomClick(item) {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataElectrical/DeleteBom?keypath=${item.keypath}`,
					method: 'delete'
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
					await this.GetBomTree()
				} else {
					this.$message.error(response.info)
				}
			},

			async bomFatherSubmit() {
				var url;
				if (this.bomFatherModel.id > 0) {
					url = `PDM/PDMDataElectrical/UpdateBom`
				} else {
					url = `PDM/PDMDataElectrical/AddBom`
				}
				this.bomFatherModel.bomtype = 1
				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.bomFatherModel
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.bomFatherClose()
					await this.loadPages()
					await this.GetBomTree()
				} else {
					this.$message.error(response.info)
				}
			},
			bomFatherClose() {
				this.bomFatherModel = {}
				this.editFatherDialog = false
			},
			UpLoadFatherBomPicCallBackPath(fileUrlItemJson) {
				this.bomFatherModel.picurl = JSON.stringify(fileUrlItemJson)
			},
			async bomhildrenSubmit() {
				var url;

				if (this.bomChildrenModel.id > 0) {
					url = `PDM/PDMDataElectrical/UpdateBom`
				} else {
					url = `PDM/PDMDataElectrical/AddBom`
				}

				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.bomChildrenModel
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.bomhildrenClose()
					await this.loadPages()
					await this.GetBomTree()
				} else {
					this.$message.error(response.info)
				}
			},
			bomhildrenClose() {
				this.bomChildrenModel = {}
				this.editBomChildrenDialog = false
			},
			UpLoadChildrenBomPicCallBackPath(fileUrlItemJson) {
				this.bomChildrenModel.picurl = JSON.stringify(fileUrlItemJson)
			},
			//默认用料
			defaultMaterialClick() {
				this.bomChildrenModel.default_material_code = ''
				this.bomChildrenModel.default_material_name = ''
				this.bomChildrenModel.default_material_qty = ''
				this.bomChildrenModel.default_material_unit = ''
				this.bomChildrenModel.default_material_specifications = ''
				this.materialDefaultComment.dialog = true
			},
			//可替换料
			replaceMaterialClick() {
				this.bomChildrenModel.replace_material_code = ''
				this.bomChildrenModel.replace_material_name = ''
				this.bomChildrenModel.replace_material_qty = ''
				this.bomChildrenModel.replace_material_unit = ''
				this.bomChildrenModel.replace_material_specifications = ''
				this.materialReplaceComment.dialog = true
			},
			//实际用料
			realMaterialClick() {
				this.bomChildrenModel.real_material_code = ''
				this.bomChildrenModel.real_material_name = ''
				this.bomChildrenModel.real_material_qty = ''
				this.bomChildrenModel.real_material_unit = ''
				this.bomChildrenModel.real_material_specifications = ''
				this.materialRealComment.dialog = true
			},
			setDefaultMaterialcallBack(inventory) {
				this.inventoryModel = inventory
				this.bomChildrenModel.default_material_code = inventory.cinvcode
				this.bomChildrenModel.default_material_name = inventory.cinvname
				this.bomChildrenModel.default_material_specifications = inventory.cinvstd
				this.bomChildrenModel.default_material_unit = inventory.unitname
				console.log(this.bomChildrenModel.default_material_unit)
			},
			setReplaceMaterialcallBack(inventory) {
				this.inventoryModel = inventory
				this.bomChildrenModel.replace_material_code = inventory.cinvcode
				this.bomChildrenModel.replace_material_name = inventory.cinvname
				this.bomChildrenModel.replace_material_specifications = inventory.cinvstd
				this.bomChildrenModel.replace_material_unit = inventory.unitname
			},
			setRealMaterialcallBack(inventory) {
				this.inventoryModel = inventory
				this.bomChildrenModel.real_material_code = inventory.cinvcode
				this.bomChildrenModel.real_material_name = inventory.cinvname
				this.bomChildrenModel.real_material_specifications = inventory.cinvstd
				this.bomChildrenModel.real_material_unit = inventory.unitname
			},
			//加工件-添加部件零件信息页面
			editProcessingClick() {
				this.equiModel.dialog = true
				this.equiModel.url = 'PDM/PDMDataElectrical/LoadProjectEquiPage?iscreateelectrical=false&'

			},
			//选择项目设备的回调函数
			async setSelectProjectEquiCallBack(selectEquiModel, equiDataHeaderList) {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataElectrical/GenerateMechanicalData?projectEquiId=${selectEquiModel.id}&type=1`,
					method: 'get',
				})
				if (response.status == 1) {
					this.changeTreePage(1)
					this.loadPages()
				}
			},
			//提交审核
			async submitAudit(item) {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataElectrical/SubmitAudit?keypath=${item.keypath}&workOrderType=1`,
					method: 'get'
				})
				if (response.status == 1) {
					this.$message.success(response.info)
				} else {
					this.$message.error(response.info)
				}

			},
			bomTypeSelectEvent(type) {
				if (type == 5) //外购件
				{
					this.materialDefaultComment.bself = true
					this.materialReplaceComment.bself = true
					this.materialRealComment.bself = true
					this.materialDefaultComment.bpurchase = true
					this.materialReplaceComment.bpurchase = true
					this.materialRealComment.bpurchase = true
				} else {
					this.materialDefaultComment.bself = true
					this.materialReplaceComment.bself = true
					this.materialRealComment.bself = true
				}
			},
			//选择外购件的供应商的回调函数
			setSelectSupplierCallBack(supplier) {
				this.inventoryModel = supplier
				this.bomChildrenModel.suppliercode = supplier.cVenCode
				this.bomChildrenModel.suppliername = supplier.cVenName
			},
			getHeaderList() {
				this.bomHeaderList = [{
						text: '操作',
						align: 'center',
						value: 'actions',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '名称',
						align: 'left',
						value: 'name',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '数量',
						align: 'left',
						value: 'real_material_qty',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '单位',
						align: 'left',
						value: 'real_material_unit',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '图号',
						align: 'left',
						value: 'piccode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: 'BOM类型',
						align: 'left',
						value: 'bomtypename',
						filterable: false,
						class: 'fixed'
					},
					{
						text: 'BOM子类型',
						align: 'left',
						value: 'bomchildrentypename',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目名称',
						align: 'left',
						value: 'project.citemname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目号',
						align: 'left',
						value: 'project.citemcode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '结构',
						align: 'left',
						value: 'keypathname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '实际用料号',
						align: 'center',
						value: 'real_material_code',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '实际用料名称',
						align: 'center',
						value: 'real_material_name',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '实际用料数量',
						align: 'center',
						value: 'real_material_qty',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '实际用料单位',
						align: 'center',
						value: 'real_material_unit',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '默认用料号',
						align: 'center',
						value: 'default_material_code',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '默认用料名称',
						align: 'center',
						value: 'default_material_name',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '默认用料数量',
						align: 'center',
						value: 'default_material_qty',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '默认用料单位',
						align: 'center',
						value: 'default_material_unit',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '可替代用料号',
						align: 'center',
						value: 'replace_material_code',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '可替代用料名称',
						align: 'center',
						value: 'replace_material_name',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '可替代用料数量',
						align: 'center',
						value: 'replace_material_qty',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '可替代用料单位',
						align: 'center',
						value: 'replace_material_unit',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '需求日期',
						align: 'center',
						value: 'assemblydate',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '外购件供应商名称',
						align: 'center',
						value: 'suppliername',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '外购件供应商编码',
						align: 'center',
						value: 'suppliercode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '外购件建议订货日期',
						align: 'center',
						value: 'buymaterialdate',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '查看BOM图纸',
						align: 'left',
						value: 'picurl',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '等级',
						align: 'center',
						value: 'level',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '审批状态',
						align: 'center',
						value: 'approval.approvalstatusname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '审批人回复',
						align: 'left',
						value: 'approval.approvaldescription',
						filterable: false,
						class: 'fixed'
					},
				]
			},
		}
	}
</script>
